-- 模块定义
---@class api
local api = {
    ---模块版本 (主版本.次版本.修订版本)
    VERSION = '1.0.0',
    ---作者备注 (更新日期 - 更新内容简述)
    AUTHOR_NOTE = '2024-04-03 - api packaging',
    ---模块名称
    MODULE_NAME = 'api module',
    ---只读模式
    READ_ONLY = false,
}

---实例对象
---@type api

local this = api

---决策模块
local decider = decider
local common = common
local utils = utils
local pairs = pairs
local table = table
local setmetatable = setmetatable
local import = import

local actor_unit = actor_unit
local quest_unit = quest_unit
local ui_unit = ui_unit
local item_unit = item_unit
local dungeon_unit = dungeon_unit
local wemix_unit = wemix_unit
local game_unit = game_unit
local cash_unit = cash_unit
local map_unit = map_unit
local skill_unit = skill_unit
local exchange_unit = exchange_unit
local login_unit = login_unit
local party_unit = party_unit
local npc_shop_unit = npc_shop_unit
local quick_unit = quick_unit
local sign_unit = sign_unit
local guild_unit = guild_unit
local mastery_unit = mastery_unit
local collection_unit = collection_unit
local creature_unit = creature_unit
local mail_unit = mail_unit
local make_unit = make_unit

local actor_ctx = actor_ctx
local npc_shop_ctx = npc_shop_ctx
local collection_ctx = collection_ctx

this.unit_list = {
    actor_unit, quest_unit, ui_unit, cash_unit, mastery_unit, creature_unit, mail_unit,
    skill_unit, exchange_unit, item_unit, login_unit, party_unit, collection_unit, make_unit,
    dungeon_unit, game_unit, sign_unit, guild_unit, quick_unit, npc_shop_unit, map_unit
}

------------------------------------------------------------------------------------
---[事件] 预载函数(重载脚本)
------------------------------------------------------------------------------------
function this.super_preload()
    -- 动态生成函数
    for _, v in pairs(this.unit_list) do
        for name, func in pairs(v) do
            -- 生成函数
            local new_func = decider.run_action_wrapper('wa_' .. name, func)
            --xxmsg(name)
            --this[name] = new_func
            this['wa_' .. name] = new_func
        end
    end
end

---------------------------------------------------------------------
-- [读取] 通用获取信息函数
--
-- @tparam   object     unit        unit对象
-- @tparam   table      cond        筛选条件 {属性, 值, 比较函数}
-- @tparam   string     fields      需要获取的字段
-- @tparam   boolean    records     获取数据个数 false 单个 true 多个
-- @tparam   table      init        初始条件 {类型, 参数}
-- @tparam   table      obj         对象
-- @treturn  table                  需要获取字段的表 或者 空表
-- @usage
-- local info = this.get_info(quest_unit, {{'id', 1, this.eq},}, {'name'}, false, {})
-- print_r(info)
---------------------------------------------------------------------
this.get_info = function(unit, cond, fields, records, init, obj)
    local ret = {}
    local type
    local arg

    if not table.is_empty(init) then
        if init[1] then
            type = init[1]
        end
        if init[2] then
            arg = init[2]
        end
    end
    local list = type and unit.list(type) or unit.list()
    local ctx = unit:new()
    for i = 1, #list do
        obj = list[i]
        if arg and ctx:init(obj, arg) or ctx:init(obj) then
            local filter = true
            for j = 1, #cond do
                local field_value = ctx[cond[j][1]](ctx)
                if not this.eval(field_value, cond[j][2], cond[j][3]) then
                    filter = false
                    break
                end
            end
            if filter then
                local t = {}
                t.obj = obj
                for j = 1, #fields do
                    t[fields[j]] = ctx[fields[j]](ctx)
                end
                ret[#ret + 1] = t
                if not records then
                    break
                end
            end
        end
    end
    ctx:delete()
    return ret
end

---------------------------------------------------------------------
-- [读取] 通用获取信息函数(增强版)
--
-- @tparam   object     unit        unit对象
-- @tparam   table      cond        筛选条件 {属性, 值, 比较函数}
-- @tparam   string     fields      需要获取的字段
-- @tparam   boolean    records     获取数据个数 false 单个 true 多个
-- @tparam   table      init        初始条件 {类型, 参数}
-- @tparam   table      obj         对象
-- @treturn  table                  需要获取字段的表 或者 空表
-- @usage
-- local info = this.get_info(quest_unit, {{'id', 1, this.eq},}, {'name'}, false, {})
-- print_r(info)
---------------------------------------------------------------------
this.get_info_ex = function(unit, cond, fields, records, init, obj)
    local ret = {}
    local type
    local arg

    if not table.is_empty(init) then
        if init[1] then
            type = init[1]
        end
        if init[2] then
            arg = init[2]
        end
    end
    local list = type and unit.list(type) or unit.list()
    local ctx = unit:new()
    for i = 1, #list do
        obj = list[i]
        if arg and ctx:init(obj, arg) or ctx:init(obj) then
            local filter = true

            if cond then
                for j = 1, #cond do
                    local field_value = ctx[cond[j][1]](ctx)
                    if not this.eval(field_value, cond[j][2], cond[j][3]) then
                        filter = false
                        break
                    end
                end
                if filter then
                    local t = {}
                    t.obj = obj
                    for j = 1, #fields do
                        t[fields[j]] = ctx[fields[j]](ctx)
                    end
                    ret[#ret + 1] = t
                    if not records then
                        break
                    end
                end
            else
                local t = {}
                t.obj = obj
                for j = 1, #fields do
                    t[fields[j]] = ctx[fields[j]](ctx)
                end
                ret[#ret + 1] = t
                if not records then
                    break
                end
            end
        end
    end
    ctx:delete()
    return ret
end
---------------------------------------------------------------------
---------------------------------------------------------------------
this.eq = function(x, y)
    return x == y
end
this.ne = function(x, y)
    return x ~= y
end
this.gt = function(x, y)
    return x > y
end
this.ge = function(x, y)
    return x >= y
end
this.lt = function(x, y)
    return x < y
end
this.le = function(x, y)
    return x <= y
end
this.of = function(x, y)
    return common.is_in_list(x, y)
end
this.contain = function(x, y)
    return string.find(x, y)
end

this.eval = function(a, b, op)
    return op(a, b)
end

------------------------------------------------------------------------------------
--                                  ACTOR 单元
------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------
---[读取] 角色是否死亡
---@usage
---api.is_actor_dead()
------------------------------------------------------------------------------------
function api.is_actor_dead()
    return local_player:is_dead()
end

---------------------------------------------------------------------
--- [读取] 角色周围信息表
---@param   init       number      当前角色0/玩家1/npc2/怪物3
---@usage
---api.get_actor_info(1)
---@return     table       角色周围信息表
---------------------------------------------------------------------
this.get_actor_info = function(init)
    local ret = {}

    ret = this.get_info_ex(actor_unit,
        {},
        { 'id', 'name_id', 'class_name', 'cx', 'cy', 'cz', 'job', 'is_dead', 'is_combat', 'name', "dist" },
        true,
        { init }
    )
    return ret
end

---------------------------------------------------------------------
--- [读取] 角色周围信息表(没有is_combat)
---@param   init       number      当前角色0/玩家1/npc2/怪物3
---@usage
---api.get_actor_info(1)
---@return     table       角色周围信息表
---------------------------------------------------------------------
this.get_actor_info_ex = function(init)
    local ret = {}

    ret = this.get_info_ex(actor_unit,
        {},
        { 'id', 'name_id', 'class_name', 'cx', 'cy', 'cz', 'job', 'is_dead', 'name', "dist" },
        true,
        { init }
    )
    return ret
end


---------------------------------------------------------------------
--- [读取] 角色当前buff列表
---@usage
---api.get_actor_buff_info()
---@return     table       角色b当前uff列表
---------------------------------------------------------------------
this.get_actor_buff_info = function()
    return actor_unit.get_local_player_buffer_list()
end

---------------------------------------------------------------------
--- [读取] 角色最大血量
---@usage
---api.get_actor_max_hp()
---@return     number       角色最大血量
---------------------------------------------------------------------
this.get_actor_max_hp = function()
    return actor_unit.local_player_max_hp()
end


-----------------------------------------------------------------------------------
---[读取] 根据传入序号获取角色数据
---@usage
---api.get_actor_data_by_idx()
---@param idx number 序号
---@return number  角色数据，0血量，10命中,12远程命中，18防御
------------------------------------------------------------------------------------
function api.get_actor_data_by_idx(idx)
    return actor_unit.get_local_player_data_byid(idx)
end

---------------------------------------------------------------------
--- [读取] 角色当前等级
---@usage
---api.get_actor_level()
---@return     number       角色当前等级
---------------------------------------------------------------------
this.get_actor_level = function()
    return actor_unit.local_player_level()
end

-----------------------------------------------------------------------------------
---[读取] 根据序号读取角色属性值
---@usage
---api.get_actor_data_by_idx()
---@param idx number 序号
------------------------------------------------------------------------------------
function api.get_actor_data_by_idx(idx)
    return actor_unit.get_local_player_data_byid(idx)
end

---------------------------------------------------------------------
--- [读取] 角色当前血量
---@usage
---api.get_actor_cur_hp()
---@return     number       角色当前血量
---------------------------------------------------------------------
this.get_actor_cur_hp = function()
    return actor_unit.local_player_hp()
end

---------------------------------------------------------------------
--- [读取] 角色当前自动类型
---@usage
---api.get_actor_auto_type()
---@return     number       角色当前自动类型
---------------------------------------------------------------------
this.get_actor_auto_type = function()
    return actor_unit.get_auto_type()
end

---------------------------------------------------------------------
--- [行为] 开启自动
---@usage
---api.auto_actor_play()
---------------------------------------------------------------------
this.auto_actor_play = function()
    return actor_unit.auto_play()
end


---------------------------------------------------------------------
--- [读取] 当前频道
---@usage
---api.get_actor_cur_channel()
---@return     number       当前频道
---------------------------------------------------------------------
this.get_actor_cur_channel = function()
    return actor_unit.get_cur_channel()
end

---------------------------------------------------------------------
--- [读取] 当前地图的频道数量
---@usage
---api.get_actor_cur_map_channel_num()
---@return     number       当前地图的频道数量
---------------------------------------------------------------------
this.get_actor_cur_map_channel_num = function()
    return actor_unit.get_cur_map_channel_num()
end

---------------------------------------------------------------------
--- [读取] 是否在频道切换页面
---@usage
---api.has_actor_channel_widget()
---@return     number       是否在频道切换页面
---------------------------------------------------------------------
this.has_actor_channel_widget = function()
    return actor_unit.is_open_channle_shift_widget()
end

---------------------------------------------------------------------
--- [读取] 角色死亡的经验条数
---@usage
---api.get_actor_death_exp_num()
---@return     number       角色死亡的经验条数
---------------------------------------------------------------------
this.get_actor_death_exp_num = function()
    return actor_unit.get_death_exp_num()
end

---------------------------------------------------------------------
--- [行为] 恢复经验条数
---@param   idx number  死亡的经验条序号,0为第一个
---@usage
---api.recovery_actor_death_exp()
---------------------------------------------------------------------
this.recovery_actor_death_exp = function(idx)
    return actor_unit.get_death_lost_exp(idx)
end
---------------------------------------------------------------------
--- [读取] 角色当前属性点点数(信念传承)
---@usage
---api.get_actor_cur_point()
---@param   idx     number     力量0/敏捷1/智力2/体力3/精神4/忍耐5
---@return     number       角色当前属性点点数
---------------------------------------------------------------------
this.get_actor_cur_point = function(idx)
    return actor_unit.get_stat_primary_level(idx)
end

---------------------------------------------------------------------
--- [读取] 角色可用的属性点数(信念传承)
---@usage
---api.get_actor_add_point_num()
---@return     number       角色可用的属性点
---------------------------------------------------------------------
this.get_actor_add_point_num = function()
    return actor_unit.get_stat_point()
end

---------------------------------------------------------------------
--- [读取] 是否在属性点页面(信念传承)
---@usage
---api.has_actor_point_widget()
---@return boolean  是否在属性点页面
---------------------------------------------------------------------
this.has_actor_point_widget = function()
    return actor_unit.is_open_stat_scene()
end

---------------------------------------------------------------------
--- [行为] 打开属性点页面(信念传承)
---@usage
---api.open_actor_point_widget()
---------------------------------------------------------------------
this.open_actor_point_widget = function()
    return actor_unit.open_stat_scene()
end
---------------------------------------------------------------------
--- [行为] 确认加点(信念传承)
---@usage
---api.confirm_actor_point()
---------------------------------------------------------------------
this.confirm_actor_point = function()
    return actor_unit.stat_decision()
end

---------------------------------------------------------------------
--- [行为] 添加角色属性点(信念传承)
---@usage
---api.add_actor_point(sel_idx,num)
---@param   sel_idx number 力量0/敏捷1/智力2/体力3/精神4/忍耐5
---@param   num     number  点数
---------------------------------------------------------------------
this.add_actor_point = function(sel_idx, num)
    return actor_unit.add_stat_primary(sel_idx, num)
end

------------------------------------------------------------------------------------
---[读取] 角色周围信息
---@usage
---api.get_actor_list(0)
---@param   type    number  当前角色0/玩家1/npc2/怪物3
---@return  table  角色周围信息列表
--------------------------------------------------------------------------------
function this.get_actor_list(type)
    local list = actor_unit.list(type)
    local actor_list = {}
    for i = 1, #list do
        if actor_ctx:init(list[i]) then
            actor_list = {
                class_id = string.format("%-X", actor_ctx:name_id()),
                class_name = string.format("%-s", actor_ctx:class_name()),
                id = string.format("%16X", actor_ctx:id()),
                x = string.format("%s", actor_ctx:cx()),
                y = string.format("%s", actor_ctx:cy()),
                z = string.format("%s", actor_ctx:cz()),
                job = string.format("%02d", actor_ctx:job()),
                is_dead = string.format("%-6s", actor_ctx:is_dead()),
                is_combat = string.format("%-6s", actor_ctx:is_combat()),
                name = string.format("%s", actor_ctx:name()),

            }
        end
    end
    return actor_list
end

function this.get_actor_list_ex(type)
    local list = actor_unit.list(type)
    local actor_list = {}
    for i = 1, #list do
        if actor_ctx:init(list[i]) then
            actor_list = {
                class_id = string.format("%-X", actor_ctx:name_id()),
                class_name = string.format("%-s", actor_ctx:class_name()),
                id = string.format("%16X", actor_ctx:id()),
                x = string.format("%s", actor_ctx:cx()),
                y = string.format("%s", actor_ctx:cy()),
                z = string.format("%s", actor_ctx:cz()),
                job = string.format("%02d", actor_ctx:job()),
                is_dead = string.format("%-6s", actor_ctx:is_dead()),
                --is_combat = string.format("%-6s", actor_ctx:is_combat()),
                name = string.format("%s", actor_ctx:name()),

            }
        end
    end
    return actor_list
end

------------------------------------------------------------------------------------
---[行为] 复活
---@usage
---api.rise_actor_man(0)
------------------------------------------------------------------
function this.rise_actor_man()
    return actor_unit.rise_man()
end

------------------------------------------------------------------------------------
---[行为] 移动
---@usage
---api.move_actor_to_xyz(tx, ty, tz)
------------------------------------------------------------------
function this.move_actor_to_xyz(tx, ty, tz)
    return actor_unit.auto_move(tx, ty, tz)
end

--------------------------------------------------------------------------------------
---[读取] 职业窗口是否打开
---@usage
---api.has_actor_class_widget()
---@return boolean 职业窗口是否打开
------------------------------------------------------------------
function this.has_actor_class_widget()
    return actor_unit.pcclass_widget_is_open()
end

--------------------------------------------------------------------------------------
---[行为] 打开职业窗口
---@usage
---api.open_actor_class_widget()
------------------------------------------------------------------
function this.open_actor_class_widget()
    return actor_unit.open_class_widget()
end

--------------------------------------------------------------------------------------
---[行为] 转职（第一个）
---@usage
---api.select_class()
---@return boolean 职业窗口是否打开
------------------------------------------------------------------
function this.select_class()
    return actor_unit.select_class("阿彻")
end

-----------------------------------------------------------------------------------
---[读取] 读取当前选择的怪物id
---@usage
---api.get_actor_cur_select_monster()
------------------------------------------------------------------------------------
function api.get_actor_cur_select_monster()
    return actor_unit.get_cur_select_monster()
end

-----------------------------------------------------------------------------------
---[行为] 选择怪物
---@usage
---api.select_actor_monster()
------------------------------------------------------------------------------------
function api.select_actor_monster(monsterId)
    return actor_unit.select_monster(monsterId)
end

------------------------------------------------------------------------------------
--                                   GAME 单元
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
---[读取] 是否为省电模式
---@usage
---api.is_game_sleep_mode()
---@return boolean 是否为省电模式
------------------------------------------------------------------
function this.is_game_sleep_mode()
    return game_unit.is_in_sleep_mode()
end

--------------------------------------------------------------------------------------
---[行为] 跳过npc对话
---@usage
---api.skip_npc_talk()
------------------------------------------------------------------
function this.skip_npc_talk()
    return game_unit.pass_dialog()
end

--------------------------------------------------------------------------------------
---[行为] 设置省电模式
---@usage
---api.set_game_sleep_mode(0)
---@param type number 退出省电0/开启省电1
------------------------------------------------------------------
function this.set_game_sleep_mode(type)
    return game_unit.set_sleep_mode(type)
end

--------------------------------------------------------------------------------------
---[行为] 设置省电模式时间
---@usage
---api.set_game_sleep_time(0)
---@param type number 不使用省电0/省电时间5
------------------------------------------------------------------
function this.set_game_sleep_time(type)
    return game_unit.set_sleep_time(type)
end

--------------------------------------------------------------------------------------
---[读取] 自动回复生命百分比阈值
---@usage
---api.get_game_hp_value()
---@return number 自动回复生命百分比阈值
------------------------------------------------------------------
function this.get_game_hp_value()
    return game_unit.get_auto_hp_value()
end

--------------------------------------------------------------------------------------
---[行为] 设置自动回复生命百分比阈值
---@usage
---api.set_game_hp_value(0)
---@param num   number  自动回复生命百分比阈值
------------------------------------------------------------------
function this.set_game_hp_value(num)
    -- 默认设置为80%
    num = num or 80

    return game_unit.set_auto_hp_value(num)
end

------------------------------------------------------------------------------------
--                                   UI 单元
------------------------------------------------------------------------------------


------------------------------------------------------------------------------------
-- [行为] 点击ui
---[行为] 点击ui
---@usage
---api.click_ui_btn(ui_id)
---@param ui_id   number  ui的id
------------------------------------------------------------------------------------
function this.click_ui_btn(ui_id)
    return ui_unit.btn_click(ui_id)
end

------------------------------------------------------------------------------------
-- [行为] 退出ui窗口
---[行为] 退出ui窗口
---@usage
---api.exit_ui_widget()
------------------------------------------------------------------------------------
function this.exit_ui_widget()
    return ui_unit.exit_widget()
end

------------------------------------------------------------------------------------
---[读取] ui按钮是否失效
---@usage
---api.is_ui_btn_disabled(ui_id)
---@param ui_id     number      ui的id
---@return boolean ui按钮是否失效
------------------------------------------------------------------------------------
function this.is_ui_btn_disabled(ui_id)
    return ui_unit.btn_is_disabled(ui_id)
end

------------------------------------------------------------------------------------
---[读取] ui按钮是否可见
---@usage
---api.is_ui_btn_visible(ui_id)
---@param ui_id     number      ui的id
---@return boolean ui按钮是否可见
------------------------------------------------------------------------------------
function this.is_ui_btn_visible(ui_id)
    return ui_unit.btn_is_visible(ui_id)
end

------------------------------------------------------------------------------------
-- [读取] 控件列表
---[读取] 控件列表
---@usage
---api.get_ui_child_control_list(ui_path)
---@param ui_path   string  ui路径
---@return table 控件列表
------------------------------------------------------------------------------------
function this.get_ui_child_control_list(ui_path)
    return ui_unit.get_child_control_list(ui_path)
end

------------------------------------------------------------------------------------
-- [读取] 控件的id
---[读取] 控件的id
---@usage
---api.get_ui_id(ui_path)
---@param ui_path   string  ui路径
---@return number   控件的id
------------------------------------------------------------------------------------
function this.get_ui_id(ui_path)
    return ui_unit.get_child_control(ui_path, true)
end

------------------------------------------------------------------------------------
-- [读取] 交互窗口是否存在
---[读取] 交互窗口是否存在
---@usage
---api.has_ui_widget(ui_id)
---@param ui_id   number  ui的id
---@return boolean   是否存在交互窗口
------------------------------------------------------------------------------------
function this.has_ui_widget(ui_id)
    return ui_unit.widget_is_visible(ui_id)
end

------------------------------------------------------------------------------------
-- [读取] ui界面
---[读取] ui界面
---@usage
---api.get_ui_parent_frame(ui_path)
---@param ui_path   string  ui路径
---@return number   未存在ui界面等于0
------------------------------------------------------------------------------------
function this.get_ui_parent_frame(ui_path)
    return ui_unit.get_top_window(ui_path, true)
end

------------------------------------------------------------------------------------
-- [读取] 复选框是否勾选
---[读取] 复选框是否勾选
---@usage
---api.is_selected_box_widget(ui_id)
---@param ui_id   number  ui的id
---@return boolean  是否勾选复选框
------------------------------------------------------------------------------------
function this.is_ui_selected_box_widget(ui_id)
    return ui_unit.check_box_is_check(ui_id)
end

------------------------------------------------------------------------------------
-- [读取] 是否有交互框
---[读取] 是否有交互框
---@usage
---api.has_ui_msg_frame()
---@return  boolean  是否有交互框
------------------------------------------------------------------------------------
function this.has_ui_msg_frame()
    return ui_unit.has_msg_box_frame()
end

------------------------------------------------------------------------------------
-- [行为] 确认交互框
---[行为] 确认交互框
---@usage
---api.confirm_ui_msg_frame()
------------------------------------------------------------------------------------
function this.confirm_ui_msg_frame()
    return ui_unit.confirm_msg_box()
end

------------------------------------------------------------------------------------
--                                    LOGIN 单元
------------------------------------------------------------------------------------
this.get_login_info = function(unit, cond, fields, records, init, obj)
    local ret = {}
    local type
    local arg

    if not table.is_empty(init) then
        if init[1] then
            type = init[1]
        end
        if init[2] then
            arg = init[2]
        end
    end
    local list = unit.role_list()
    local ctx = unit:new()
    for i = 1, #list do
        obj = list[i]
        if arg and ctx:init(obj, arg) or ctx:init(obj) then
            local filter = true


            if cond then
                for j = 1, #cond do
                    local field_value = ctx[cond[j][1]](ctx)
                    if not this.eval(field_value, cond[j][2], cond[j][3]) then
                        filter = false
                        break
                    end
                end
                if filter then
                    local t = {}
                    t.obj = obj
                    for j = 1, #fields do
                        t[fields[j]] = ctx[fields[j]](ctx)
                    end
                    ret[#ret + 1] = t
                    if not records then
                        break
                    end
                end
            else
                local t = {}
                t.obj = obj
                for j = 1, #fields do
                    t[fields[j]] = ctx[fields[j]](ctx)
                end
                ret[#ret + 1] = t
                if not records then
                    break
                end
            end
        end
    end
    ctx:delete()
    return ret
end


---------------------------------------------------------------------
---[读取] 获取登录列表的角色信息
---@usage
---api.get_role_info()
---@return     table       获取登录列表的角色信息
---------------------------------------------------------------------
this.get_role_info = function()
    local ret = {}
    ret = this.get_login_info(login_unit,
        {},
        { 'id', 'job', 'level', 'name', },
        true,
        {}
    )
    return ret
end

------------------------------------------------------------------------------------
--                                  EXCHANGE 单元
------------------------------------------------------------------------------------






------------------------------------------------------------------------------------
--                                  TEAM 单元
------------------------------------------------------------------------------------






------------------------------------------------------------------------------------
--                                  SHOP 单元
------------------------------------------------------------------------------------

---------------------------------------------------------------------
---[读取] 获取可购买的商店物品信息
---@usage
---api.get_shop_info()
---@return     table       获取可购买的商店物品信息
---------------------------------------------------------------------
function this.get_shop_info()
    local ret = {}
    ret = this.get_info(npc_shop_unit,
        {},
        { 'id', 'res_ptr', 'res_id', 'type', 'price', 'name' },
        true
    )
    return ret
end

--function this.get_shop_info()
--    local list = npc_shop_unit.list()
--    local npc_shop_list = {}
--    local temp_list = {}
--    for i = 1, #list do
--        if npc_shop_ctx:init(list[i]) then
--            temp_list = {
--                res_ptr = npc_shop_ctx:res_ptr(),
--                id      = npc_shop_ctx:id(),
--                res_id  = npc_shop_ctx:res_id(),
--                type    = npc_shop_ctx:type(),
--                price   = npc_shop_ctx:price(),
--                --can_buy = npc_shop_ctx:can_buy(),
--                name    = npc_shop_ctx:name()
--            }
--        end
--        table.insert(npc_shop_list, temp_list)
--    end
--    return npc_shop_list
--end

-----------------------------------------------------------------------------------
---[读取]获取购买弹窗数量
---@usage
---api.get_shop_buy_pop_num()
------------------------------------------------------------------------------------
function api.get_shop_buy_num()
    return npc_shop_unit.get_cur_set_buy_num()
end

-----------------------------------------------------------------------------------
---[读取] 检查物品是否可购买
---@usage
---api.check_shop_can_buy()
------------------------------------------------------------------------------------
function api.check_shop_can_buy(item_id)
    return npc_shop_unit.check_item(item_id)
end

-----------------------------------------------------------------------------------
---[行为] 打开购买弹窗
---@usage
---api.open_buy_pop()
------------------------------------------------------------------------------------
function api.open_shop_buy_pop(item_id)
    return npc_shop_unit.select_buy_item(item_id)
end

-----------------------------------------------------------------------------------
---[读取] 当前npc窗口的购买列表
---@usage
---api.get_shop_cur_list()
------------------------------------------------------------------------------------
function api.get_shop_cur_list()
    -- 如果不在商店页面返回空表
    if api.has_shop_frame() == 0 then
        return {}
    end
    return npc_shop_unit.cur_shop_item_list()
end

---------------------------------------------------------------------
---[读取]  是否在商店页面
---@usage
---api.has_shop_frame()
---@return number  不在商店页面等于0
---------------------------------------------------------------------
this.has_shop_frame = function()
    return npc_shop_unit.get_npc_store_scene()
end



---------------------------------------------------------------------
---[行为]  打开商店
---@param   npc_id       number     npc的id
---@usage
---api.open_shop(npc_id)
---------------------------------------------------------------------
this.open_shop = function(npc_id)
    return npc_shop_unit.open_shop(npc_id)
end

---------------------------------------------------------------------
---[行为] 商店购买物品
---@param   npc_id       number     npc的id
---@param   item_id       number     物品的id
---@param   num       number     购买数量
---@usage
---api.buy_shop_item(npc_id,item_id,num)
---------------------------------------------------------------------
this.buy_shop_item = function(npc_id, item_id, num)
    return npc_shop_unit.buy_item(npc_id, item_id, num)
end



------------------------------------------------------------------------------------
--                                  MAKE 单元
------------------------------------------------------------------------------------
---------------------------------------------------------------------
---[读取] 获取制造信息
---@param nType number  普通制造0/其他制造术待测试
---@usage
---api.get_make_info()
---@return     table       获取制造信息列表
---------------------------------------------------------------------
this.get_make_info = function(nType)
    local ret = {}
    local cond = {}
    if nType ~= nil then table.insert(cond, { 'type', nType, this.eq }) end
    ret = this.get_info_ex(make_unit,
        cond,
        { 'id', 'type', 'make_res_id', 'level', 'name' },
        true,
        {}
    )
    return ret
end

---------------------------------------------------------------------
---[行为] 制造是否受限
---@param   make_id       number     制造id
---@usage
---api.is_make_item_limitation(make_id)
---@return boolean 制造的物品是否受限，true为受限
---------------------------------------------------------------------
function this.is_make_item_limitation(make_id)
    return make_unit.can_restriction_make(make_id)
end

-----------------------------------------------------------------------------------
---[行为] 打开制造窗口
---@usage
---api.open_make_widget()
------------------------------------------------------------------------------------
function api.open_make_widget()
    return make_unit.open_make_scene()
end

------------------------------------------------------------------------------------
--                                  CASH 单元
------------------------------------------------------------------------------------


---------------------------------------------------------------------
---[读取] 获取商城信息列表
---@usage
---api.get_cash_info()
---@return     table       获取商城信息列表
---------------------------------------------------------------------
this.get_cash_info = function()
    local ret = {}
    ret = this.get_info_ex(cash_unit,
        {},
        { 'res_ptr', 'id', 'money_type', 'price', 'cur_buy_num', 'max_buy_num', 'name' },
        true,
        {}
    )
    return ret
end

---------------------------------------------------------------------
---[行为] 商城购买物品
---@param   id       number     物品的购买id
---@param   num       number     购买数量
---@usage
---api.buy_cash_item(id,num)
---------------------------------------------------------------------
this.buy_cash_item = function(id, num)
    return cash_unit.buy(id, num)
end


------------------------------------------------------------------------------------
--                                  QUEST 单元
------------------------------------------------------------------------------------


---------------------------------------------------------------------
---[读取] 获取任务信息列表
---@param   init       number      所有类型-1/可接0/已接1/完成2/
---@param   nType       number      主线0/支线1
---@usage
---api.get_quest_info(1,0)
---@return     table       任务信息表
---------------------------------------------------------------------
this.get_quest_info = function(init, nType)
    local ret = {}
    ret = this.get_info(quest_unit,
        { { 'type', nType, this.eq }, },
        { 'id', 'name', 'step_id', 'type', 'status', 'daily_quest_level', 'cur_tar_num', 'max_tar_num', 'chapter' },
        true,
        { init }
    )
    return ret
end


---------------------------------------------------------------------
-- [读取] 获取每日任务信息函数
--
-- @tparam   object     unit        unit对象
-- @tparam   table      cond        筛选条件 {属性, 值, 比较函数}
-- @tparam   string     fields      需要获取的字段
-- @tparam   boolean    records     获取数据个数 false 单个 true 多个
-- @tparam   table      init        初始条件 {类型, 参数}
-- @tparam   table      obj         对象
-- @treturn  table                  需要获取字段的表 或者 空表
-- @usage
-- local info = this.get_daily_info(quest_unit, {{'id', 1, this.eq},}, {'name'}, false, {})
-- print_r(info)
---------------------------------------------------------------------
this.get_daily_info = function(unit, cond, fields, records, init, obj)
    local ret = {}
    local type
    local arg

    if not table.is_empty(init) then
        if init[1] then
            type = init[1]
        end
        if init[2] then
            arg = init[2]
        end
    end
    local list = type and unit.daily_list(type) or unit.daily_list()
    local ctx = unit:new()
    for i = 1, #list do
        obj = list[i]
        if arg and ctx:init(obj, arg) or ctx:init(obj) then
            local filter = true

            if cond then
                for j = 1, #cond do
                    local field_value = ctx[cond[j][1]](ctx)
                    if not this.eval(field_value, cond[j][2], cond[j][3]) then
                        filter = false
                        break
                    end
                end
                if filter then
                    local t = {}
                    t.obj = obj
                    for j = 1, #fields do
                        t[fields[j]] = ctx[fields[j]](ctx)
                    end
                    ret[#ret + 1] = t
                    if not records then
                        break
                    end
                end
            else
                local t = {}
                t.obj = obj
                for j = 1, #fields do
                    t[fields[j]] = ctx[fields[j]](ctx)
                end
                ret[#ret + 1] = t
                if not records then
                    break
                end
            end
        end
    end
    ctx:delete()
    return ret
end

---------------------------------------------------------------------
---[读取] 获取每日任务信息列表
---@param   init       number      所有-1/可接0/已接1/完成/2
---@usage
---api.get_daily_quest_info()
---@return     table       任务信息表
---------------------------------------------------------------------
this.get_daily_quest_info = function(init)
    local ret = {}
    ret = this.get_daily_info(quest_unit,
        {},
        { 'res_ptr', 'id', 'step_id', 'type', 'status', 'daily_quest_level', 'cur_tar_num', 'max_tar_num', "name" },
        true,
        { init }
    )
    return ret
end

------------------------------------------------------------------------------------
---[读取] 获取当前自动任务id
---@usage
---api.get_quest_auto_id()
---@return  number  获取当前自动任务id
------------------------------------------------------------------------------------
function this.get_quest_auto_id()
    return quest_unit.get_cur_auto_quest_id()
end

------------------------------------------------------------------------------------
---[读取] 获取任务列表
---@usage
---api.get_quest_list(nType)
---@param nType number 可接任务0/已接任务1/已完成任务2
---@return  table  获取任务列表
------------------------------------------------------------------------------------
function this.get_quest_list(nType)
    local list = quest_unit.list(nType)
    local quest_list = {}
    local quest_temp
    for i = 1, #list do
        if quest_ctx:init(list[i]) then
            quest_temp = {
                obj = list[i],
                id = quest_ctx:id(),
                step_id = quest_ctx:step_id(),
                type = quest_ctx:type(),
                status = quest_ctx:status(),
                daily_quest_level = quest_ctx:daily_quest_level(),
                name = quest_ctx:name(),
                max_tar_num = quest_ctx:max_tar_num(),
                cur_tar_num = quest_ctx:cur_tar_num(),
            }
        end
        table.insert(quest_list, quest_temp)
    end

    return quest_list
end

------------------------------------------------------------------------------------
---[读取] 任务是否在自动
---@usage
---@param id number 任务id
---api.is_quest_auto()
---@return boolean 是否为自动状态
------------------------------------------------------------------------------------
function this.is_quest_auto(id)
    return quest_unit.quest_is_auto(id)
end

------------------------------------------------------------------------------------
---[读取] 是否有任务传送挂件
---@usage
---api.has_quest_teleport_popup()
---@return boolean 是否有任务传送挂件
------------------------------------------------------------------------------------
function this.has_quest_teleport_popup()
    return quest_unit.has_quest_teleport_popup()
end

------------------------------------------------------------------------------------
---[行为] 确认任务传送
---@usage
---api.confirm_quest_transfer()
------------------------------------------------------------------------------------
function this.confirm_quest_transfer()
    return quest_unit.confirm_quest_teleport()
end

------------------------------------------------------------------------------------
---[行为] 接受任务
---@usage
---@param id number 任务id
---api.accept_quest(id)
------------------------------------------------------------------------------------
function this.accept_quest(id)
    return quest_unit.accept(id)
end

------------------------------------------------------------------------------------
---[行为] 接受每日任务
---@usage
---@param id number 任务id
---api.accept_quest_daily(id)
------------------------------------------------------------------------------------
function this.accept_quest_daily(id)
    return quest_unit.accept_daily(id)
end

------------------------------------------------------------------------------------
---[行为] 提交任务
---@usage
---@param id number 任务id
---api.complete_quest(id)
------------------------------------------------------------------------------------
function this.complete_quest(id)
    return quest_unit.complate(id)
end

------------------------------------------------------------------------------------
---[行为] 提交每日任务
---@usage
---@param id number 任务id
---api.complete_quest_daily(id)
------------------------------------------------------------------------------------
function this.complete_quest_daily(id)
    return quest_unit.complate_daily(id)
end

------------------------------------------------------------------------------------
---[行为] 自动执行任务
---@usage
---@param id number 任务id
---api.auto_quest(id)
------------------------------------------------------------------------------------
function this.auto_quest(id)
    return quest_unit.auto_quest(id)
end

------------------------------------------------------------------------------------
---[行为] 自动执行每日任务
---@usage
---@param id number 任务id
---api.auto_quest_daily(id)
------------------------------------------------------------------------------------
function this.auto_quest_daily(id)
    return quest_unit.auto_daily_quest(id)
end

------------------------------------------------------------------------------------
--                                  CREATURE 单元
------------------------------------------------------------------------------------


------------------------------------------------------------------------------------
---[读取] 坐骑、翅膀、武器图的列表
---@usage
---@param init number 坐骑0，翅膀1，武器图2
---api.get_creature_list(id)
---@return table 坐骑、翅膀、武器图的表
------------------------------------------------------------------------------------
function api.get_creature_info(init)
    local ret = {}
    ret = this.get_info_ex(creature_unit,
        {},
        { 'id', 'type', 'num', 'quality', 'is_used', 'name' },
        true,
        { init }
    )
    return ret
end

------------------------------------------------------------------------------------
---[读取] 坐骑、翅膀、武器图的列表
---@usage
---@param type number 坐骑0，翅膀1，武器图2
---api.get_creature_list(id)
---@return number 坐骑、翅膀、武器图的id
------------------------------------------------------------------------------------
function this.get_creature_list(type)
    return creature_unit.list(type)
end

------------------------------------------------------------------------------------
---[读取] 获取翅膀列表
---@usage
---api.get_creature_flying_list()
---@return  table  获取翅膀列表
------------------------------------------------------------------------------------
function this.get_creature_flying_list()
    local list = creature_unit.list(1)
    local flying_temp
    local flying_list = {}

    for i = 1, #list do
        if creature_ctx:init(list[i]) then
            flying_temp = {
                id = creature_ctx:id(),
                type = creature_ctx:type(),
                num = creature_ctx:num(),
                quality = creature_ctx:quality(),
                is_used = creature_ctx:is_used(),
                name = creature_ctx:name()
            }
        end
        table.insert(flying_list, flying_temp)
    end

    return flying_list
end

------------------------------------------------------------------------------------
---[读取] 获取坐骑列表
---@usage
---api.get_creature_horse_list()
---@return  table  获取坐骑列表
------------------------------------------------------------------------------------
function this.get_creature_horse_list()
    local list = creature_unit.list(0)
    local horse_list = {}
    local horse_temp
    for i = 1, #list do
        if creature_ctx:init(list[i]) then
            horse_temp = {
                id = creature_ctx:id(),
                type = creature_ctx:type(),
                num = creature_ctx:num(),
                quality = creature_ctx:quality(),
                is_used = creature_ctx:is_used(),
                name = creature_ctx:name()
            }
        end
        table.insert(horse_list, horse_temp)
    end

    return horse_list
end

------------------------------------------------------------------------------------
---[读取] 获取武器外形列表
---@usage
---api.get_creature_weapon_list()
---@return  table  获取武器外形列表
------------------------------------------------------------------------------------
function this.get_creature_weapon_list()
    local list = creature_unit.list(2)
    local weapon_list = {}
    local weapon_temp
    for i = 1, #list do
        if creature_ctx:init(list[i]) then
            weapon_temp = {
                id = creature_ctx:id(),
                type = creature_ctx:type(),
                num = creature_ctx:num(),
                quality = creature_ctx:quality(),
                is_used = creature_ctx:is_used(),
                name = creature_ctx:name()
            }
        end
        table.insert(weapon_list, weapon_temp)
    end

    return weapon_list
end

------------------------------------------------------------------------------------
---[行为] 使用坐骑、翅膀、武器图
---@usage
---@param id number 坐骑0/翅膀1/武器图2
---api.use_creature(id)
------------------------------------------------------------------------------------
function this.use_creature(id)
    return creature_unit.use_creature(id)
end

------------------------------------------------------------------------------------
---[读取] 是否在召唤页面
---@usage
---api.has_creature_scene()
---@return boolean 是否在召唤页面
------------------------------------------------------------------------------------
function this.has_creature_scene()
    return creature_unit.has_gacha_scene()
end

------------------------------------------------------------------------------------
---[行为] 关闭召唤页面
---@usage
---api.close_creature_scene()
------------------------------------------------------------------------------------
function this.close_creature_scene()
    return creature_unit.close_gacha_scene()
end

------------------------------------------------------------------------------------
--                                  SKILL 单元
------------------------------------------------------------------------------------


------------------------------------------------------------------------------------
---[读取] 获取技能列表
---@usage
---api.get_skill_list()
---@return  table  获取技能列表
------------------------------------------------------------------------------------
function this.get_skill_list()
    local list = skill_unit.list()
    local skill_list = {}
    local skill_temp

    for i = 1, #list do
        local obj = list[i]
        if skill_ctx:init(obj) then
            skill_temp = {
                id = skill_ctx:id(),
                group_id = skill_ctx:group_id(),
                --cd = skill_ctx:cooling_time(),
                --mp = skill_ctx:mp(),
                is_study = skill_ctx:is_study(),
                name = skill_ctx:name(),
                level = skill_ctx:level(),

            }
        end
        table.insert(skill_list, skill_temp)
    end

    return skill_list
end

------------------------------------------------------------------------------------
--                                  QUICK 单元
------------------------------------------------------------------------------------

---------------------------------------------------------------------
---[读取] 获取当前快捷栏的信息
---@usage
---api.get_quick_info()
---@return     table       获取当前快捷栏的信息
---------------------------------------------------------------------
this.get_quick_info = function()
    local list = {}
    local quick_list = {}

    for i = 0, 7 do
        quick_list = {
            set_obj_id = quick_unit.get_quick_item_id(i),
            set_type = quick_unit.get_quick_item_type(i),
            is_active = quick_unit.quick_is_active(i),
        }

        table.insert(list, quick_list)
    end
    return list
end

------------------------------------------------------------------------------------
---[行为] 设置技能快捷键
---@usage
---api.set_skill_quick()
------------------------------------------------------------------------------------
function this.set_skill_quick(sel_idx, skill_id)
    return quick_unit.set_quick_skill(sel_idx, skill_id)
end

------------------------------------------------------------------------------------
--                                  ITEM 单元
------------------------------------------------------------------------------------

---------------------------------------------------------------------
---[读取] 获取背包中的信息
---@param   init       number      所有物品0/已穿戴物品1
---@param   type       number      物品类型，装备0/材料1/药水2
---@usage
---api.get_item_info(0)
---@return     table       背包信息表
---------------------------------------------------------------------
this.get_item_info = function(init, type, equip_type, quality, equip_job, enhanced_level)
    local ret = {}
    local cond = {}
    -- 根据传入的参数动态构建筛选条件
    if type ~= nil then table.insert(cond, { 'type', type, this.eq }) end
    if equip_type ~= nil then table.insert(cond, { 'equip_type', equip_type, this.eq }) end
    if quality ~= nil then table.insert(cond, { 'quality', quality, this.eq }) end
    if equip_job ~= nil then table.insert(cond, { 'equip_job', equip_job, this.eq }) end
    if enhanced_level ~= nil then table.insert(cond, { 'enhanced_level', enhanced_level, this.eq }) end
    ret = this.get_info_ex(item_unit,
        --{ { 'type', nType, this.eq }, },
        cond,
        { 'id', 'res_ptr', 'res_id', 'type', 'num', 'quality', 'weight', 'equip_type', 'equip_job', 'enhanced_level',
            'name', "is_expiration", "is_damage" },--, "expiration"
        true,
        { init }
    )

    return ret
end


-----------------------------------------------------------------------------------
---[读取] 是否有仓库窗口
---@usage
---api.has_item_storage_widget()
---@return number 未在仓库窗口返回0
------------------------------------------------------------------------------------
function api.has_item_storage_widget()
    return item_unit.get_ware_house_scene()
end

-----------------------------------------------------------------------------------
---[行为] 打开仓库窗口
---@usage
---api.open_item_storage()
---@param npc_id number npc的id
------------------------------------------------------------------------------------
function api.open_item_storage(npc_id)
    return item_unit.open_storage(npc_id)
end

-----------------------------------------------------------------------------------
---[读取] 存仓物品列表
---@usage
---api.get_item_storage_list()
---@return table 存仓物品列表，未打开存仓窗口为空表
------------------------------------------------------------------------------------
function api.get_item_storage_list()
    return item_unit.storage_list()
end

-----------------------------------------------------------------------------------
---[行为] 储存物品
---@usage
---api.storage_item()
---@param id number 物品id
------------------------------------------------------------------------------------
function api.storage_item(id)
    return item_unit.storage_item(id)
end

-----------------------------------------------------------------------------------
---[行为] 取出存仓物品
---@usage
---api.extract_item()
---@param id number 存仓物品id
------------------------------------------------------------------------------------
function api.extract_item(id)
    return item_unit.retrieve_item(id)
end

-----------------------------------------------------------------------------------
---[读取] 背包物品列表
---@usage
---api.get_item_bag_list(0)
---@param type number 所有物品0/已装备的物品1
---@return  table  背包物品列表
------------------------------------------------------------------------------------
function this.get_item_bag_list(type)
    local list = item_unit.list(type)
    local item_list = {}
    local item_temp

    for i = 1, #list do
        if item_ctx:init(list[i]) then
            item_temp = {
                obj = list[i],
                id = item_ctx:id(),
                res_id = item_ctx:res_id(),
                type = string.format('%02d', item_ctx:type()),
                num = tonumber(string.format('%06d', item_ctx:num())),
                quality = string.format('%02d', item_ctx:quality()),
                weight = string.format('%06d', item_ctx:weight()),
                equip_type = string.format('%03d', item_ctx:equip_type()),
                equip_job = string.format('%03d', item_ctx:equip_job()),           -- 副武器好像不一样
                enhanced_level = string.format('%02d', item_ctx:enhanced_level()), --强化等级
                name = string.format('%s', item_ctx:name()),
                is_damage = string.format('%s', item_ctx:is_damage()),
                is_expiration = item_ctx:is_expiration(),
            }
            table.insert(item_list, item_temp)
        end
    end

    return item_list
end

-----------------------------------------------------------------------------------
---[读取] 背包当前负重
---@usage
---api.get_item_bag_cur_weight()
---@return number 背包当前负重
------------------------------------------------------------------------------------
function this.get_item_bag_cur_weight()
    return item_unit.get_inventory_weight()
end

-----------------------------------------------------------------------------------
---[读取] 背包最大负重
---@usage
---api.get_item_bag_max_weight()
---@return number 背包最大负重
------------------------------------------------------------------------------------
function this.get_item_bag_max_weight()
    return item_unit.get_inventory_max_weight()
end

-----------------------------------------------------------------------------------
---[读取] 强化装备
---@usage
---api.enhanced_item_equip(equip_id, item_id, equip_name)
---@param equip_id number  装备id
---@param item_id  number   卷轴id
------------------------------------------------------------------------------------
function this.enhanced_item_equip(equip_id, item_id)
    return item_unit.enhancement_equip(equip_id, item_id)
end

-----------------------------------------------------------------------------------
---[读取] 是否在强化页面
---@usage
---api.get_item_enhancement()
---@return number 未在强化页面返回0
------------------------------------------------------------------------------------
function this.get_item_enhancement()
    return item_unit.get_enhancement_scene()
end

-----------------------------------------------------------------------------------
---[行为] 打开单个强化页面
---@usage
---api.open_item_enhancement()
------------------------------------------------------------------------------------
function this.open_item_enhancement()
    return item_unit.open_enhancement_scene()
end

-----------------------------------------------------------------------------------
---[读取] 是否打开背包
---@usage
---api.is_item_open_bag()
---@return number 是否打开背包
------------------------------------------------------------------------------------
function this.is_item_open_bag()
    return item_unit.is_open_inventory()
end

-----------------------------------------------------------------------------------
---[行为] 打开背包
---@usage
---api.open_item_bag()
------------------------------------------------------------------------------------
function this.open_item_bag()
    return item_unit.open_inventory()
end

-----------------------------------------------------------------------------------
---[行为] 关闭背包
---@usage
---api.close_item_bag()
------------------------------------------------------------------------------------
function this.close_item_bag()
    return item_unit.close_inventory()
end

-----------------------------------------------------------------------------------
---[读取] 是否在分解页面
---@usage
---api.is_item_open_decompose()
---@return boolean 是否在分解页面
------------------------------------------------------------------------------------
function this.is_item_open_decompose()
    return item_unit.is_open_decompose()
end

-----------------------------------------------------------------------------------
---[行为] 打开分解页面
---@usage
---api.open_item_decompose()
------------------------------------------------------------------------------------
function this.open_item_decompose()
    return item_unit.open_decompose()
end

-----------------------------------------------------------------------------------
---[行为] 分解物品
---@usage
---api.decompose_item(item_list)
---@param item_list     table   物品的列表
---@field  item_list.item_id number 物品的id
------------------------------------------------------------------------------------
function this.decompose_item(item_list)
    return item_unit.decomopse(item_list)
end

-----------------------------------------------------------------------------------
---[行为] 使用物品
---@usage
---api.use_item(item_id,1)
---@param item_id number 物品id
------------------------------------------------------------------------------------
function this.use_item(item_id)
    return item_unit.use_item(item_id, 1)
end

-----------------------------------------------------------------------------------
---[行为] 是否有开箱确认页面
---@usage
---api.has_item_box_popup()
---@return boolean 是否有开箱确认页面
------------------------------------------------------------------------------------
function this.has_item_box_popup()
    return item_unit.has_Item_Box_popup()
end

-----------------------------------------------------------------------------------
---[行为] 点击最大并且确认箱子
---@usage
---api.confirm_item_add_max()
------------------------------------------------------------------------------------
function this.confirm_item_add_max()
    return item_unit.add_max_and_confirm()
end

-----------------------------------------------------------------------------------
---[行为] 使用箱子
---@usage
---api.use_item_box(0xB80564,1)
---@param item_id number 箱子id
---@param num   number    开箱个数
---@param sel_idx   number  选择序号,0是第一个
------------------------------------------------------------------------------------
function this.use_item_box(item_id, num, sel_idx)
    return item_unit.use_box(item_id, num, sel_idx)
end

-----------------------------------------------------------------------------------
---[读取] 是否在使用箱子成功页面
---@usage
---api.has_item_acquire_popup()
---@return boolean 在使用箱子成功页面
------------------------------------------------------------------------------------
function this.has_item_acquire_popup()
    return item_unit.has_acquire_popup()
end

-----------------------------------------------------------------------------------
---[行为] 关闭使用箱子成功页面
---@usage
---api.close_item_acquire_popup()
------------------------------------------------------------------------------------
function this.close_item_acquire_popup()
    return item_unit.close_acquire_popup()
end

-----------------------------------------------------------------------------------
---[读取] 是否有自动装备的窗口
---@usage
---api.has_item_auto_equip_widget()
------------------------------------------------------------------------------------
function this.has_item_auto_equip_widget()
    return item_unit.has_auto_equip_widget()
end

-----------------------------------------------------------------------------------
---[行为] 自动装备(弹窗式)
---@usage
---api.use_item_auto_equip()
------------------------------------------------------------------------------------
function this.use_item_auto_equip()
    return item_unit.auto_equip()
end

-----------------------------------------------------------------------------------
---[读取] 读取货币
---@usage
---api.get_item_money()
---@param type number 铜钱3/钻石1
---@return number  钻石或铜钱
------------------------------------------------------------------------------------
function this.get_item_money(type)
    return item_unit.get_money_byid(type)
end

------------------------------------------------------------------------------------
this.get_dun_info = function(unit, cond, fields, records, init, obj)
    local ret = {}
    local type
    local arg

    if not table.is_empty(init) then
        if init[1] then
            type = init[1]
        end
        if init[2] then
            arg = init[2]
        end
    end
    local list = type and unit.list(type) or unit.dungeon_list()
    local ctx = unit:new()
    for i = 1, #list do
        obj = list[i]
        if arg and ctx:init(obj, arg) or ctx:init(obj) then
            local filter = true

            if cond then
                for j = 1, #cond do
                    local field_value = ctx[cond[j][1]](ctx)
                    if not this.eval(field_value, cond[j][2], cond[j][3]) then
                        filter = false
                        break
                    end
                end
                if filter then
                    local t = {}
                    t.obj = obj
                    for j = 1, #fields do
                        t[fields[j]] = ctx[fields[j]](ctx)
                    end
                    ret[#ret + 1] = t
                    if not records then
                        break
                    end
                end
            else
                local t = {}
                t.obj = obj
                for j = 1, #fields do
                    t[fields[j]] = ctx[fields[j]](ctx)
                end
                ret[#ret + 1] = t
                if not records then
                    break
                end
            end
        end
    end
    ctx:delete()
    return ret
end

------------------------------------------------------------------------------------
--                                  DUNGEON 单元
------------------------------------------------------------------------------------

---------------------------------------------------------------------
---[读取] 获取副本信息表
---@usage
---api.get_main_dungeon_info()
---@return     table       任务信息表
---------------------------------------------------------------------
this.get_main_dungeon_info = function()
    local ret = {}
    ret = this.get_dun_info(dungeon_unit,
        {},
        { 'name', 'id' },
        true,
        {}
    )
    return ret
end

-----------------------------------------------------------------------------------
---[读取] 是否打开副本窗口
---@usage
---api.has_dungeon_widget()
---@return boolean  是否打开副本窗口
------------------------------------------------------------------------------------
function this.has_dungeon_widget()
    return dungeon_unit.is_open_dungeon_widget()
end

-----------------------------------------------------------------------------------
---[读取] 获取所有主副本的id表
---@usage
---api.get_dungeon_list()
---@return table  获取所有主副本的id表
------------------------------------------------------------------------------------
function this.get_dungeon_list()
    return dungeon_unit.dungeon_list()
end

-----------------------------------------------------------------------------------
---[读取] 获取子副本的id表
---@usage
---api.get_dungeon_sub_list()
---@param  main_dungeon_id number  主副本的id,需要转16进制
---@return table  获取子副本的id表
------------------------------------------------------------------------------------
function this.get_dungeon_sub_list(main_dungeon_id)
    return dungeon_unit.dungeon_stage_list(main_dungeon_id)
end

-----------------------------------------------------------------------------------
---[读取] 是否有传送窗口
---@usage
---api.has_dungeon_transfer_widget()
---@return boolean  是否有传送窗口
------------------------------------------------------------------------------------
function this.has_dungeon_transfer_widget()
    return dungeon_unit.has_enter_zone_popup()
end

-----------------------------------------------------------------------------------
---[行为] 打开副本窗口
---@usage
---api.open_dungeon_widget()
------------------------------------------------------------------------------------
function this.open_dungeon_widget()
    return dungeon_unit.open_dungeon_widget()
end

-----------------------------------------------------------------------------------
---[行为] 请求副本
---@usage
---api.request_dungeon(main_id,sub_id,action)
------------------------------------------------------------------------------------
function this.request_dungeon(main_id, sub_id, action)
    return dungeon_unit.req_dungeon_action(main_id, sub_id, action)
end

-- 请求副本行为（action 1 请求数剧 = 选择， 2 请求进入副本出zone_popup）
--

-----------------------------------------------------------------------------------
---[行为] 确认进入副本按钮
---@usage
---api.confirm_dungeon_btn()
------------------------------------------------------------------------------------
function this.confirm_dungeon_btn()
    return dungeon_unit.enter_dungeon()
end

------------------------------------------------------------------------------------
--                                  MAP 单元
------------------------------------------------------------------------------------


---------------------------------------------------------------------
--- [读取] 获取当前大地图的id
---@usage
---api.get_map_main_id()
---@return     number       获取当前大地图的id
---------------------------------------------------------------------
this.get_map_main_id = function()
    return actor_unit.main_map_id()
end

---------------------------------------------------------------------
--- [读取] 获取当前地图的id
---@usage
---api.get_map_cur_id()
---@return     number       获取当前地图的id
---------------------------------------------------------------------
this.get_map_cur_id = function()
    return actor_unit.map_id()
end

------------------------------------------------------------------------------------
---[读取] 当前地图名字
---@usage
---api.get_map_cur_name()
---@return string   当前地图中文名
------------------------------------------------------------------------------------
function api.get_map_cur_name()
    return actor_unit.map_name()
end

---------------------------------------------------------------------
--- [读取] 获取地图的传送信息表
---@usage
---api.get_map_info()
---@return     table       获取地图的传送信息表
---------------------------------------------------------------------
this.get_map_info = function()
    local ret = {}

    ret = this.get_info_ex(map_unit,
        nil,
        { 'id', 'name' },
        true,
        {}
    )
    return ret
end

------------------------------------------------------------------------------------
---[行为] 传送
---@usage
---api.transfer_map_to(map_id)
---@param   map_id  number  地图的传送id
------------------------------------------------------------------------------------
function api.transfer_map_to(map_id)
    return map_unit.transfer_to_map(map_id)
end

------------------------------------------------------------------------------------
--                                    SIGN 单元
------------------------------------------------------------------------------------


------------------------------------------------------------------------------------
---[读取] 获取签到列表
---@usage
---api.get_sign_check_in_list()
---@return  table 获取签到列表
------------------------------------------------------------------------------------
function api.get_sign_check_in_list()
    return sign_unit.get_can_receive_list()
end

------------------------------------------------------------------------------------
---[行为] 领取签到奖励
---@usage
---api.receive_sign_check_in_reward(reward_id,idx)
---@param     reward_id     number      可领取签到奖励的id
---@param      idx          number      可领取签到奖励的序号
------------------------------------------------------------------------------------
function api.receive_sign_check_in_reward(reward_id, idx)
    return sign_unit.get_attendance_reward(reward_id, idx)
end

------------------------------------------------------------------------------------
---[读取] 获取活动的列表
---@usage
---api.get_sign_event_list()
---@return  table 获取活动的列表
------------------------------------------------------------------------------------
function api.get_sign_event_list()
    return sign_unit.get_tar_event_list()
end

------------------------------------------------------------------------------------
---[读取] 获取子活动能领取的id
---@usage
---api.get_sign_event_list()
---@param   event_id    number  领取活动的id
---@return  table 获取子活动的id
------------------------------------------------------------------------------------
function api.get_sign_can_receive_event(event_id)
    return sign_unit.get_can_receive_list(event_id)
end

------------------------------------------------------------------------------------
---[读取] 获取子活动能领取的序号列表
---@usage
---api.get_sign_reward_idx_list(event_id)
---@param   event_id    number  领取活动的id
---@return  table 获取子活动能领取的序号列表
------------------------------------------------------------------------------------
function api.get_sign_reward_idx_list(event_id)
    return sign_unit.get_reward_idx_list(event_id)
end

------------------------------------------------------------------------------------
---[读取] 获取通行证的列表
---@usage
---api.get_sign_pass_list()
---@return  table 获取通行证的列表
------------------------------------------------------------------------------------
function api.get_sign_pass_list()
    return sign_unit.get_season_pass_list()
end

------------------------------------------------------------------------------------
---[读取] 是否有通行证窗口
---@usage
---api.has_sign_pass_widget()
---@return  boolean 是否有通行证窗口
------------------------------------------------------------------------------------
function api.has_sign_pass_widget()
    return sign_unit.season_pass_popup_is_open()
end

------------------------------------------------------------------------------------
---[读取] 是否有活动窗口
---@usage
---api.has_sign_event_widget()
---@return  boolean 是否有活动窗口
------------------------------------------------------------------------------------
function api.has_sign_event_widget()
    return sign_unit.is_open_event_popup()
end

------------------------------------------------------------------------------------
---[读取] 是否能领取通行证奖励
---@usage
---api.is_sign_pass_receive_reward(reward_id)
---@param reward_id     number  奖励id
---@return  boolean 是否能领取通行证奖励
------------------------------------------------------------------------------------
function api.is_sign_pass_receive_reward(reward_id)
    return sign_unit.season_has_receive_reward(reward_id)
end

------------------------------------------------------------------------------------
---[行为] 打开活动窗口
---@usage
---api.open_sign_event_widget()
------------------------------------------------------------------------------------
function api.open_sign_event_widget()
    return sign_unit.open_event_popup()
end

------------------------------------------------------------------------------------
---[行为] 关闭活动窗口
---@usage
---api.close_sign_event_widget()
------------------------------------------------------------------------------------
function api.close_sign_event_widget()
    return sign_unit.close_event_popup()
end

------------------------------------------------------------------------------------
---[行为] 领取通行证奖励
---@usage
---api.receive_sign_pass_reward()
---@param  reward_id     number     领取奖励的id
------------------------------------------------------------------------------------
function api.receive_sign_pass_reward(reward_id)
    return sign_unit.get_season_reward(reward_id)
end

------------------------------------------------------------------------------------
---[行为] 领取活动奖励
---@usage
---api.receive_sign_event_reward()
---@param  reward_id     number     领取活动奖励的id
---@param  idx     number     领取序号
------------------------------------------------------------------------------------
function api.receive_sign_event_reward(reward_id, idx)
    return sign_unit.get_event_reward(reward_id, idx)
end

------------------------------------------------------------------------------------
--                                    MASTERY 单元
------------------------------------------------------------------------------------


---------------------------------------------------------------------
---[读取] 获取熟练度信息列表

---@usage
---api.get_mastery_info()
---@return     table       获取熟练度信息列表
---------------------------------------------------------------------
this.get_mastery_info = function()
    local ret = {}
    ret = this.get_info_ex(mastery_unit,
        {},
        { 'id', "level", "class_id", "is_study" },
        true,
        {}
    )
    return ret
end

------------------------------------------------------------------------------------
---[读取] 当前熟练度等级
---@usage
---api.get_mastery_cur_level()
---@return      number   当前熟练度等级
------------------------------------------------------------------------------------
function this.get_mastery_cur_level()
    return mastery_unit.get_mastery_level()
end

------------------------------------------------------------------------------------
---[读取] 是否打开熟练度界面
---@usage
---api.is_open_mastery_widget()
---@return      boolean   是否打开熟练度界面
------------------------------------------------------------------------------------
function this.is_open_mastery_widget()
    return mastery_unit.is_open_mastery_scene()
end

------------------------------------------------------------------------------------
---[读取] 打开熟练度界面
---@usage
---api.open_mastery_widget()
------------------------------------------------------------------------------------
function this.open_mastery_widget()
    return mastery_unit.open_mastery_scene()
end

------------------------------------------------------------------------------------
---[读取] 是否打开学习窗口
---@usage
---api.is_open_mastery_study_widget()
---@return      boolean   是否打开学习窗口
------------------------------------------------------------------------------------
function this.is_open_mastery_study_widget()
    return mastery_unit.is_open_mastery_popup()
end

------------------------------------------------------------------------------------
---[行为] 打开学习窗口
---@usage
---api.open_mastery_study_widget()
------------------------------------------------------------------------------------
function this.open_mastery_study_widget()
    return mastery_unit.open_mastery_popup()
end

------------------------------------------------------------------------------------
---[行为] 关闭学习窗口
---@usage
---api.close_mastery_study_widget()
------------------------------------------------------------------------------------
function this.close_mastery_study_widget()
    return mastery_unit.close_mastery_popup()
end

------------------------------------------------------------------------------------
---[行为] 提升武器熟练度
---@usage
---api.study_mastery()
------------------------------------------------------------------------------------
function this.study_mastery(id)
    return mastery_unit.study(id)
end

------------------------------------------------------------------------------------
---[行为] 检测是否已学武器熟练度
---@usage
---api.is_mastery_study()
------------------------------------------------------------------------------------
function this.is_mastery_study(id)
    return mastery_unit.mastery_is_study(id)
end

------------------------------------------------------------------------------------
--                                   COLLECTION 单元
------------------------------------------------------------------------------------



------------------------------------------------------------------------------------
---[读取] 读取收藏信息
---@usage
---api.get_collection_info()
---@return      table   收藏信息表
------------------------------------------------------------------------------------
this.get_collection_info = function()
    local collection_list = {}
    local list = collection_unit.list()
    --xxmsg('宝鉴数：' .. #list)
    -- 遍历列表中的每个收藏单位
    for _, obj in ipairs(list) do
        if collection_ctx:init(obj) then
            local sub_num = collection_ctx:num()
            local max = 0   -- 初始化max为0
            local unit = {} -- 创建一个新的表来存储当前收藏单位的信息

            -- 存储当前收藏单位的基本信息
            unit.obj = obj
            unit.id = collection_ctx:id()
            unit.num = collection_ctx:num()
            unit.finish_num = collection_ctx:finish_num()
            unit.is_finish = collection_ctx:is_finish()
            unit.name = collection_ctx:name()
            -- 存储子项,需要收藏物品的信息
            local sub_items = {}
            for j = 0, sub_num - 1 do
                local sub_bag_item_id = collection_ctx:sub_bag_item_id(j)
                if sub_bag_item_id ~= nil and sub_bag_item_id >= max then
                    local sub_item = {} -- 创建一个新的表来存储当前子项的信息
                    sub_item.idx = j
                    sub_item.sub_obj = collection_ctx:sub_obj(j)
                    sub_item.status = collection_ctx:sub_status(j)
                    sub_item.item_num = collection_ctx:sub_item_num(j)
                    sub_item.item_max_num = collection_ctx:sub_item_max_num(j)
                    sub_item.bag_item_id = sub_bag_item_id
                    sub_item.item_res_id = collection_ctx:sub_item_res_id(j)
                    sub_item.item_type = collection_ctx:sub_item_type(j)
                    sub_item.equip_enhanced_lv = collection_ctx:sub_item_enhanced_level(j)
                    sub_item.is_finish = collection_ctx:sub_item_is_finish(j)
                    sub_item.item_name = collection_ctx:sub_item_name(j)
                    -- 将子项信息添加到子项表中
                    table.insert(sub_items, sub_item)
                    -- 更新max为当前最大的sub_bag_item_id
                    max = sub_bag_item_id
                end
            end
            unit.sub_items = sub_items -- 将子项表存储到当前收藏单位的信息中
            max = 0                    -- 重置max为0，用于下一轮比较
            -- 将当前收藏单位的信息添加到 CollectionData 表中
            table.insert(collection_list, unit)
        end
    end

    return collection_list
end

------------------------------------------------------------------------------------
---[读取] 判断是否打开收藏窗口
---@usage
---api.is_collection_open_widget()
---@return  boolean     判断是否打开收藏窗口
------------------------------------------------------------------------------------
function api.is_collection_open_widget()
    return collection_unit.is_open_collection_scene()
end

------------------------------------------------------------------------------------
---[行为] 打开收藏窗口
---@usage
---api.open_collection_widget()
------------------------------------------------------------------------------------
function api.open_collection_widget()
    return collection_unit.open_collection_scene()
end

------------------------------------------------------------------------------------
---[行为] 领取活动奖励
---@usage
---api.collection()
---@param  unit_id     number     收藏分支的id
---@param  item_idx     number     收藏物品序号
------------------------------------------------------------------------------------
function api.collection_item(unit_id, item_idx)
    return collection_unit.collection(unit_id, item_idx)
end

------------------------------------------------------------------------------------
--                                  MAIL 单元
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
---[行为] 打开邮箱页面
---@usage
---api.open_mail_widget()
------------------------------------------------------------------------------------
function api.open_mail_widget()
    return mail_unit.open_mail_scene()
end

------------------------------------------------------------------------------------
---[读取] 获取当前邮箱页面
---@usage
---api.get_mail_frame(0)
---@return number 服务器0,角色1,公会2,系统3
------------------------------------------------------------------------------------
function api.get_mail_frame(nType)
    return mail_unit.get_select_mail_type(nType)
end

------------------------------------------------------------------------------------
---[行为] 切换邮箱页面
---@usage
---api.get_mail_frame(0)
---@param nType number 服务器0,角色1,公会2,系统3
------------------------------------------------------------------------------------
function api.change_mail_frame(nType)
    return mail_unit.change_mail_type(nType)
end

------------------------------------------------------------------------------------
-- [行为] 一键领取邮件
------------------------------------------------------------------------------------
function api.change_mail_frame(nType)
    return mail_unit.get_all_server_maill(nType)
end

------------------------------------------------------------------------------------
---[行为] 是否有邮件
---@usage
---api.has_mail(0)
---@param nType number 角色0,服务器1，公会2,系统3
------------------------------------------------------------------------------------
function api.has_mail(nType)
    return mail_unit.has_server_mail(nType)
end

------------------------------------------------------------------------------------
---[读取] 是否在邮箱页面
---@usage
---api.has_mail_scene()
---@return boolean 是否在邮箱页面
------------------------------------------------------------------------------------
function api.has_mail_scene()
    return mail_unit.mail_scene_is_open()
end

------------------------------------------------------------------------------------
---[读取] 邮件数量
---@usage
---api.get_mail_num()
---@return number 邮件数量
------------------------------------------------------------------------------------
function api.get_mail_num()
    return mail_unit.get_sys_mail_num()
end

------------------------------------------------------------------------------------
--                                  IMPRINT 单元
------------------------------------------------------------------------------------

------------------------------------------------------------------------------------
---[读取] 魔力注入列表
---@usage
---api.get_imprint_list()
---@return table 魔力注入列表
------------------------------------------------------------------------------------
function api.get_imprint_list()
    if api.get_actor_level() < 35 then
        return {}
    end
    return imprint_unit.list()
end

-----------------------------------------------------------------------------------
---[读取] 魔力注入
---@usage
---api.imprint_item()
------------------------------------------------------------------------------------
function api.imprint_item(id)
    return imprint_unit.imprint(id)
end

------------------------------------------------------------------------------------
--                                  GUILD 单元
------------------------------------------------------------------------------------

------------------------------------------------------------------------------------
this.get_guild_info = function(unit, cond, fields, records, init, obj)
    local ret = {}
    local type
    local arg

    if not table.is_empty(init) then
        if init[1] then
            type = init[1]
        end
        if init[2] then
            arg = init[2]
        end
    end
    local list = type and unit.guild_list(type) or unit.guild_list()
    local ctx = unit:new()
    for i = 1, #list do
        obj = list[i]
        if arg and ctx:init(obj, arg) or ctx:init(obj) then
            local filter = true

            if cond then
                for j = 1, #cond do
                    local field_value = ctx[cond[j][1]](ctx)
                    if not this.eval(field_value, cond[j][2], cond[j][3]) then
                        filter = false
                        break
                    end
                end
                if filter then
                    local t = {}
                    t.obj = obj
                    for j = 1, #fields do
                        t[fields[j]] = ctx[fields[j]](ctx)
                    end
                    ret[#ret + 1] = t
                    if not records then
                        break
                    end
                end
            else
                local t = {}
                t.obj = obj
                for j = 1, #fields do
                    t[fields[j]] = ctx[fields[j]](ctx)
                end
                ret[#ret + 1] = t
                if not records then
                    break
                end
            end
        end
    end
    ctx:delete()
    return ret
end

-----------------------------------------------------------------------------------
---[读取] 公会推荐信息表
---@usage
---api.get()
------------------------------------------------------------------------------------
function api.get_guild_recommend_list()
    local ret = {}
    ret = this.get_guild_info(guild_unit,
        {},
        { 'member_num', 'join_status', 'id', 'name', 'level', 'master_name', },
        true,
        {}
    )
    return ret
end

-----------------------------------------------------------------------------------
---[行为] 打开公会窗口
---@usage
---api.open_guild_widget()
------------------------------------------------------------------------------------
function api.open_guild_widget()
    return guild_unit.open_guild_widget()
end

-----------------------------------------------------------------------------------
---[读取] 是否能公会捐赠
---@usage
---api.can_guild_donation()
---@param id number 捐赠id
------------------------------------------------------------------------------------
function api.can_guild_donation(id)
    return guild_unit.can_donation(id)
end

-----------------------------------------------------------------------------------
---[行为] 捐赠
---@usage
---@param id number 捐赠的id
---api.donate_guild()
------------------------------------------------------------------------------------
function api.donate_guild(id)
    return guild_unit.donation(id)
end

-----------------------------------------------------------------------------------
---[读取] 当前公会任务状态
---@usage
---api.get_guild_cur_quest_status()
---@return number 公会任务状态，未完成2
------------------------------------------------------------------------------------
function api.get_guild_cur_quest_status()
    return guild_unit.get_cur_guild_quest_status()
end

---------------------------------------------------------------------
---[读取] 获取公会信息列表
---@usage
---api.get_guild_list()
---@return     table       公会信息列表
---------------------------------------------------------------------
this.get_guild_list = function()
    if api.get_guild_id() == 0 then
        return {}
    end
    local ret = {}
    local guild_quest_list = guild_unit.get_guild_quest_id_list()
    for i = 1, #guild_quest_list do
        local quest_id = guild_quest_list[i]
        local quest_name = guild_unit.get_guild_quest_name_byid(quest_id)
        local ret_temp = {
            id = quest_id,
            name = quest_name
        }
        table.insert(ret, ret_temp)
    end
    return ret
end

-----------------------------------------------------------------------------------
---[行为] 申请加入公会
---@usage
---api.apply_guild()
---@param id  number 公会id
------------------------------------------------------------------------------------
function api.apply_guild(id)
    return guild_unit.apply_guild(id)
end

-----------------------------------------------------------------------------------
---[读取] 完成的公会任务数量
---@usage
---api.get_guild_complete_quest_num()
------------------------------------------------------------------------------------
function api.get_guild_complete_quest_num()
    return guild_unit.get_quest_complate_num()
end

-----------------------------------------------------------------------------------
---[读取] 当前公会等级
---@usage
---api.get_guild_level()
------------------------------------------------------------------------------------
function api.get_guild_level()
    return guild_unit.get_guild_level()
end

-----------------------------------------------------------------------------------
---[读取] 是否开启公会窗口
---@usage
---api.is_guild_open_widget()
------------------------------------------------------------------------------------
function api.is_guild_open_widget()
    return guild_unit.is_open_guild_widget()
end

-----------------------------------------------------------------------------------
---[读取] 当前公会名
---@usage
---api.get_guild_name()
------------------------------------------------------------------------------------
function api.get_guild_name()
    return guild_unit.get_guild_name()
end

-----------------------------------------------------------------------------------
---[读取] 当前公会id
---@usage
---api.get_guild_id()
---@return number   公会id，0为未加入公会
------------------------------------------------------------------------------------
function api.get_guild_id()
    return guild_unit.get_guild_id()
end

-----------------------------------------------------------------------------------
---[行为] 接受公会任务
---@usage
---api.accept_guild_quest()
------------------------------------------------------------------------------------
function api.accept_guild_quest(id)
    return guild_unit.accept_quest(id)
end

-----------------------------------------------------------------------------------
---[行为] 刷新公会任务
---@usage
---api.refresh_guild_quest()
------------------------------------------------------------------------------------
function api.refresh_guild_quest()
    return guild_unit.refresh_quest()
end

-----------------------------------------------------------------------------------
---[行为] 提交公会任务
---@usage
---api.complete_guild_quest()
------------------------------------------------------------------------------------
function api.complete_guild_quest()
    return guild_unit.complate_quest()
end

-----------------------------------------------------------------------------------
---[行为] 公会任务id列表
---@usage
---api.get_guild_quest_id_list()
------------------------------------------------------------------------------------
function api.get_guild_quest_id_list()
    if api.get_guild_level() < 2 then
        return {}
    end
    -- 保存公会任务的列表
    local quest_t = {}
    local list = guild_unit.get_guild_quest_id_list()
    for i = 1, #list do
        local quest_id = list[i]
        local result = {
            id = quest_id,
            status = api.get_guild_cur_quest_status(quest_id),
            name = guild_unit.get_guild_quest_name_byid(quest_id),

        }
        table.insert(quest_t, result)
    end

    return quest_t
end

-----------------------------------------------------------------------------------
---[行为] 检查公会捐赠物品
---@usage
---api.check_guild_item(idx)
---@param idx number 捐赠序号0铜钱/1烟水晶/2钻石
------------------------------------------------------------------------------------
function api.check_guild_item(idx)
    return guild_unit.check_item(idx)
end


-----------------------------------------------------------------------------------
---[读取] 读取铸币计算器弹窗
---@usage
---api.get_wemix_calculator_pop()
------------------------------------------------------------------------------------
function api.get_wemix_calculator_pop()
    return wemix_unit.get_calculator_popup()
end

-----------------------------------------------------------------------------------
---[读取] 读取铸币网页窗口
---@usage
---api.get_wemix_web_widget()
------------------------------------------------------------------------------------
function api.get_wemix_web_widget()
    return wemix_unit.get_wemix_web_browser()
end

-----------------------------------------------------------------------------------
---[读取] 读取钱包窗口
---@usage
---api.get_wemix_widget()
------------------------------------------------------------------------------------
function api.get_wemix_widget()
    return wemix_unit.get_mute_secene()
end

-----------------------------------------------------------------------------------
---[读取] 读取当前选择的铸币序号
---@usage
---api.get_wemix_cur_coin_idx()
------------------------------------------------------------------------------------
function api.get_wemix_cur_coin_idx()
    return wemix_unit.get_cur_select_token_idx()
end

-----------------------------------------------------------------------------------
---[读取] 读取钱包ID
---@usage
---api.get_wemix_wallet_id()
------------------------------------------------------------------------------------
function api.get_wemix_wallet_id()
    return wemix_unit.get_wallet_id()
end

-----------------------------------------------------------------------------------
---[读取] 根据序号读取钱包中的币数量
---@usage
---api.get_wemix_coin_num_by_idx()
---@param idx number 序号0-8
------------------------------------------------------------------------------------
function api.get_wemix_coin_num_by_idx(idx)
    return wemix_unit.get_token_money_byidx(idx)
end

-----------------------------------------------------------------------------------
---[行为] 选择铸币种类
---@usage
---api.select_wemix_coin()
------------------------------------------------------------------------------------
function api.select_wemix_coin()
    return wemix_unit.select_token()
end
-----------------------------------------------------------------------------------
---[行为] 选择钱包类型
---@usage
---
---api.select_wemix_wallet_type()
---@param nType number 钱包类型:0 = wemix_play,3 = play_wallet
------------------------------------------------------------------------------------
function api.select_wemix_wallet_type(nType)
    return wemix_unit.set_wallet_type(nType)
end

------------------------------------------------------------------------------------
---[内部] 将对象转换为字符串
--
---@local
---@treturn      string                     模块名称
------------------------------------------------------------------------------------
function api.__tostring()
    return this.MODULE_NAME
end

------------------------------------------------------------------------------------
---[内部] 防止动态修改(this.READ_ONLY值控制)
--
---@local
---@tparam       table     t                被修改的表
---@tparam       any       k                要修改的键
---@tparam       any       v                要修改的值
------------------------------------------------------------------------------------
function api.__newindex(t, k, v)
    if this.READ_ONLY then
        error('attempt to modify read-only table')
        return
    end
    rawset(t, k, v)
end

------------------------------------------------------------------------------------
---[内部] 设置item的__index元方法指向自身
--
---@local
------------------------------------------------------------------------------------
api.__index = api

------------------------------------------------------------------------------------
---[构造] 创建一个新的实例
--
---@local
---@tparam       table     args             可选参数，用于初始化新实例
---@treturn      table                      新创建的实例
------------------------------------------------------------------------------------
function api:new(args)
    local new = {}
    ---预载函数(重载脚本时)
    if this.super_preload then
        this.super_preload()
    end
    ---将args中的键值对复制到新实例中
    if args then
        for key, val in pairs(args) do
            new[key] = val
        end
    end
    ---设置元表
    return setmetatable(new, api)
end

-------------------------------------------------------------------------------------
---返回实例对象
-------------------------------------------------------------------------------------
return api:new()

-------------------------------------------------------------------------------------
